import service from '@/utils/request.js'

/**
 * 添加文本知识
 */
export const addTextKnowledge = (data) => {
  return service({
    url: '/knowledge/text',
    method: 'POST',
    data: {
      roleId: data.roleId,
      title: data.title,
      content: data.content
    },
    setting: {
      isToken: true
    }
  })
}

/**
 * 上传文件知识
 */
export const addFileKnowledge = (roleId, file) => {
  const formData = new FormData()
  formData.append('roleId', roleId)
  formData.append('file', file)
  
  return service({
    url: '/knowledge/file',
    method: 'POST',
    data: formData,  // 使用data而不是params
    setting: {
      isToken: true
    }
  })
}

/**
 * 获取角色知识库列表
 */
export const getKnowledgeByRole = (roleId) => {
  return service({
    url: `/knowledge/role/${roleId}`,
    method: 'GET',
    setting: {
      isToken: true
    }
  })
}

/**
 * 删除知识文档
 */
export const deleteKnowledge = (documentId, roleId) => {
  return service({
    url: `/knowledge/${documentId}?roleId=${roleId}`,
    method: 'DELETE',
    setting: {
      isToken: true
    }
  })
}

/**
 * 搜索知识库
 */
export const searchKnowledge = (roleId, query, maxResults = 5) => {
  return service({
    url: '/knowledge/search',
    method: 'GET',
    params: {
      roleId,
      query,
      maxResults
    },
    setting: {
      isToken: true
    }
  })
}

/**
 * 清空角色知识库
 */
export const clearRoleKnowledge = (roleId) => {
  return service({
    url: `/knowledge/role/${roleId}/clear`,
    method: 'DELETE',
    setting: {
      isToken: true
    }
  })
}